<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\CarWarrantyDao;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use think\facade\Db;
use function GuzzleHttp\Psr7\str;

class CarWarrantyServices extends BaseServices
{
    public function __construct(CarWarrantyDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where,bool $is_page){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        $pattern = '/\.([a-zA-z0-9]+)$/';
        foreach ($list['list'] as &$val){
//            $exp = explode(DIRECTORY_SEPARATOR, $val['file_url']);
//            $val['name'] = end($exp);
            if(preg_match($pattern,$val['file_url'],$matches)){
                $val['extension'] = $matches[1];
            }else{
                $val['extension'] = '';
            }
            $val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
        }
        return $list;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     */
    public function delete(int $id){
        $res = $this->dao->update($id,['is_del'=>1,'delete_time'=>time()]);
        if(!$res) throw new AdminException('删除失败,请稍候再试');
        return '删除成功';
    }

    /**
     * 保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id,array $data){
        $info = $this->dao->get($id);
        if($id){
            if(!$info) throw new AdminException('未查询到记录');
            if(!$this->dao->update($id,$data)) throw new AdminException('提交失败或您什么也没修改');
        }else{
            unset($data['id']);
            $data['add_time'] = time();
            if(!$this->dao->save($data)) throw new AdminException('提交失败');
            $car = Db::name('car_info')->where(['id'=>$data['car_id']])->find();
            if($car){
                $insurance_date = $car['insurance_date'] ? strtotime('+1 year',$car['insurance_date']) : strtotime('+1 year');
                $commercial_insurance_date = $car['commercial_insurance_date'] ? strtotime('+1 year',$car['commercial_insurance_date']) : strtotime('+1 year');
                Db::name('car_info')->where(['id'=>$data['car_id']])->update(['insurance_date'=>$insurance_date,'commercial_insurance_date'=>$commercial_insurance_date]);
            }
        }
        return true;
    }
}
